#ifndef ERRORDIALOGMANAGER_H
#define ERRORDIALOGMANAGER_H

#include "source/logger.h"
#include <QObject>
#include <QQmlApplicationEngine>
#include <QSemaphore>
#include <QTimer>
#include <QVariant>

class ErrorDialogManager : public QObject {
    Q_OBJECT
    Q_PROPERTY(QString title READ title NOTIFY titleChanged)
    Q_PROPERTY(QString detail READ detail NOTIFY detailChanged)


signals:
    void showErrorDialog();
    void showInfoDialog();
    void titleChanged();
    void detailChanged();

public:
    static ErrorDialogManager& instance() {
        static ErrorDialogManager instance_;
        return instance_;
    }
    QString title()  const {return title_;};
    QString detail() const {return detail_;};

    void showNonBlockingError(const QString &title, const QString &message)
    {
        LogDebug << "showNonBlockingError  ...." ; 
        title_ = title;
        detail_ = message;

        emit titleChanged();
        emit detailChanged();
        emit showErrorDialog();
    }

    void showNonBlockingInfo(const QString &title, const QString &message)
    {
        LogDebug << "showNonBlockingInfo  ...." ;
        title_ = title;
        detail_ = message;

        emit titleChanged();
        emit detailChanged();
        emit showInfoDialog();
    }


    // void initialize(QQmlApplicationEngine *engine) {
    //     m_engine = engine;
    //     QObject *rootObject = m_engine->rootObjects().first();
    //     qDebug() << "Root object:" << rootObject->objectName();

    //     // Print all children of rootObject
    //     const QObjectList children = rootObject->children();
    //     for (QObject *child : children) {
    //         qDebug() << "Child object:" << child << "with objectName:" << child->objectName();
    //     }

    //     QObject *rootItem = rootObject->findChild<QObject*>("rootItem");
    //     qDebug() << "Root item:" << rootItem;
    //     if (rootItem) {-------------------
    //         connect(rootItem, SIGNAL(dialogAccepted(int)), this, SLOT(onDialogAccepted(int)));
    //     } else {
    //         qWarning() << "rootItem not found!";
    //     }
    // }

    // Q_INVOKABLE void showNonBlockingError(const QString &title, const QString &message) {


    //     // QObject *rootObject = m_engine->rootObjects().first();
    //     // QObject *rootItem = rootObject->findChild<QObject*>("rootItem");
    //     // if (rootItem) {
    //     //     QMetaObject::invokeMethod(rootItem, "showNonBlockingError",
    //     //                               Q_ARG(QVariant, QVariant(title)), Q_ARG(QVariant, QVariant(message)));
    //     // } else {
    //     //     qWarning() << "rootItem not found!";
    //     // }
    // }

    // int showBlockingError(const QString &title, const QString &message, const QStringList &buttons, int timeout = 0) {
    //     QSemaphore semaphore;
    //     m_semaphore = &semaphore;

    //     QVariantList buttonList;
    //     for (const QString &button : buttons) {
    //         buttonList << button;
    //     }

    //     QObject *rootObject = m_engine->rootObjects().first();
    //     QObject *rootItem = rootObject->findChild<QObject*>("rootItem");
    //     if (rootItem) {
    //         QMetaObject::invokeMethod(rootItem, "showBlockingError",
    //                                   Q_ARG(QVariant, QVariant(title)), Q_ARG(QVariant, QVariant(message)), Q_ARG(QVariant, buttonList));
    //     } else {
    //         qWarning() << "rootItem not found!";
    //     }

    //     if (timeout > 0) {
    //         QTimer timer;
    //         timer.setSingleShot(true);
    //         QObject::connect(&timer, &QTimer::timeout, [&]() {
    //             if (m_semaphore->available() == 0) {
    //                 m_selected    Q_INVOKABLE void showNonBlockingError(const QString &title, const QString &message) {
    //     QObject *rootObject = m_engine->rootObjects().first();
    //     QObject *rootItem = rootObject->findChild<QObject*>("rootItem");
    //     if (rootItem) {
    //         QMetaObject::invokeMethod(rootItem, "showNonBlockingError",
    //                                   Q_ARG(QVariant, QVariant(title)), Q_ARG(QVariant, QVariant(message)));
    //     } else {
    //         qWarning() << "rootItem not found!";
    //     }
    // }

    // int showBlockingError(const QString &title, const QString &message, const QStringList &buttons, int timeout = 0) {
    //     QSemaphore semaphore;
    //     m_semaphore = &semaphore;

    //     QVariantList buttonList;
    //     for (const QString &button : buttons) {
    //         buttonList << button;
    //     }

    //     QObject *rootObject = m_engine->rootObjects().first();
    //     QObject *rootItem = rootObject->findChild<QObject*>("rootItem");
    //     if (rootItem) {
    //         QMetaObject::invokeMethod(rootItem, "showBlockingError",
    //                                   Q_ARG(QVariant, QVariant(title)), Q_ARG(QVariant, QVariant(message)), Q_ARG(QVariant, buttonList));
    //     } else {
    //         qWarning() << "rootItem not found!";
    //     }

    //     if (timeout > 0) {
    //         QTimer timer;
    //         timer.setSingleShot(true);
    //         QObject::connect(&timer, &QTimer::timeout, [&]() {
    //             if (m_semaphore->available() == 0) {
    //                 m_selectedButton = -1;
    //                 m_semaphore->release();
    //             }
    //         });Button = -1;
    //                 m_semaphore->release();
    //             }
    //         });
    //         timer.start(timeout);
    //     }

    //     m_semaphore->acquire();
    //     return m_selectedButton;
    // }

    // private slots:
    // void onDialogAccepted(int button) {
    //     m_selectedButton = button;
    //     if (m_semaphore) {
    //         m_semaphore->release();
    //     }
    // }

private:
    QString title_;
    QString detail_;

    ErrorDialogManager(QObject *parent = nullptr):QObject(parent){};
    ~ErrorDialogManager(){};
    ErrorDialogManager& operator=(const ErrorDialogManager& rhs) = delete;
    // QQmlApplicationEngine *m_engine;
    // QSemaphore *m_semaphore;
    // int m_selectedButton;
};

#endif // ERRORDIALOGMANAGER_H
